﻿#include "GUIGeometryBaseDialog.h"

#include "GUIFrame/MainWindow.h"

#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGeometryData.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoCommand.h"

#include "PickDataProvider/PickedData.h"
#include "PickDataProvider/GUIPickInfo.h"
#include "PickDataProvider/PickedDataProvider.h"

#include <QMessageBox>
#include <QTableWidgetItem>
#include <QSpacerItem>

namespace GUI {

    GUIGeometryBaseDialog::GUIGeometryBaseDialog(Interface::FITKAbsGeoCommand* obj, Core::FITKActionOperator* oper, QWidget* parent) : GUIDialogBase(parent),
        _obj(obj), _oper(oper)
    {
        _geometryData = FITKAPP->getGlobalData()->getGeometryData<Interface::FITKGeoCommandList>();
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);//去掉对话框右上角问号
                //创建工具，用来显示拉伸方向
        EventOper::GraphEventOperator*operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("OperGraphTool");
        if (operGraph != nullptr)
        {
            m_direction = operGraph->createGraphTool(EventOper::GraphToolType::ToolDirection);
        }
    }

    GUIGeometryBaseDialog::~GUIGeometryBaseDialog()
    {
        //所有的类在执行完毕后，将清空对3维的操作
        GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        if (provider == nullptr)return;
        provider->clearPickedData();//清空拾取数据
        //拾取对象 关闭
        //拾取方式 空
        GUI::GUIPickInfoStru pickInfo = GUI::GUIPickInfo::GetPickInfo();
        pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType(GUI::GUIPickInfo::POBJNone);
        pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod(GUI::GUIPickInfo::PickMethod::PMNone);
        //设置拾取方式
        GUI::GUIPickInfo::SetPickInfo(pickInfo);


        //窗口关闭时，清理方向显示
        EventOper::GraphEventOperator*operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("OperGraphTool");
        if (operGraph != nullptr)
        {
            operGraph->deleteToolObject(m_direction);
        }
    }

    void GUIGeometryBaseDialog::init()
    {

    }

    void GUIGeometryBaseDialog::enablePickSig(int pickObjType, int pickMethodType)
    {
        //获取拾取器，绑定拾取结束信号
        GraphData::PickedDataProvider* pickProvider = GraphData::PickedDataProvider::getInstance();
        //清除拾取对象所有的连接
        if (pickProvider != nullptr)
        {
            QObject::disconnect(pickProvider, SIGNAL(sig_pickedOver()), nullptr, nullptr);
            connect(pickProvider, &GraphData::PickedDataProvider::sig_pickedOver, this, &GUIGeometryBaseDialog::pickFinishedOper);
            connect(pickProvider, &GraphData::PickedDataProvider::sig_pickedData, this, &GUIGeometryBaseDialog::pickInProcess);
        }
        GUI::GUIPickInfoStru pickInfo = GUI::GUIPickInfo::GetPickInfo();
        pickInfo._pickObjType = GUI::GUIPickInfo::PickObjType(pickObjType);
        pickInfo._pickMethod = GUI::GUIPickInfo::PickMethod(pickMethodType);
        //设置拾取方式
        GUI::GUIPickInfo::SetPickInfo(pickInfo);
    }

    bool GUIGeometryBaseDialog::createObjData()
    {
        if (!_geometryData || !_obj) return false;

        QString name = this->getNameText();
        if (_geometryData->getDataByName(name)) {
            QMessageBox::warning(this, "", tr("\"%1\" already exists and cannot be overwritten.").arg(name), QMessageBox::Ok);
            return false;
        }

        _obj->setDataObjectName(name);
        this->getDataFormWidget();
        if (_obj->update()==false)
        {
            //创建失败不将该数据append
            return false;
        }
        _geometryData->appendDataObj(_obj);
        return true;
    }

    bool GUIGeometryBaseDialog::editObjData()
    {
        if (!_geometryData || !_obj) return false;
        QString name = this->getNameText();
        if (_obj->getDataObjectName() != name && _geometryData->getDataByName(name)) {
            QMessageBox::warning(this, "", tr("\"%1\" already exists and cannot be overwritten.").arg(name), QMessageBox::Ok);
            return false;
        }
        _obj->setDataObjectName(name);
        this->getDataFormWidget();
        _obj->update();

        return true;
    }

    QString GUIGeometryBaseDialog::getNameText()
    {
        return QString();
    }

    void GUIGeometryBaseDialog::createGeometryObj(int type)
    {
        Interface::FITKInterfaceGeometryFactory* geoFactory = Interface::FITKInterfaceGeometryFactory::getInstance();
        if (!geoFactory) return;
        _obj = geoFactory->createCommand(Interface::FITKGeoEnum::FITKGeometryComType(type));
        if (!_obj) return;
        _isCreate = true;
    }

    void GUIGeometryBaseDialog::getDataFormWidget()
    {

    }

    void GUIGeometryBaseDialog::pickFinishedOper()
    {
        ////获取拾取数据
        //GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        //if (provider == nullptr)return;
        //disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(pickFinishedOper()));
        //disconnect(provider, SIGNAL(sig_pickedData()), this, SLOT(pickInProcess()));
        //QList<GraphData::PickedData*> pickList = provider->getPickedList();
        //QList<int> idList = {};
        //for (GraphData::PickedData* pickData : pickList) {
        //    if (pickData == nullptr)continue;
        //    idList.append(pickData->getPickedDataObjId());
        //}
        ////获取拾取的数据
        //if (idList.isEmpty()) return;
        //Interface::FITKAbsGeoCommand* pickObj = _geometryData->getDataByID(idList.at(0));
        //QString name = pickObj->getDataObjectName();
    }

    void GUIGeometryBaseDialog::pickInProcess()
    {
        m_pick_num = 0;
        GraphData::PickedDataProvider* provider = GraphData::PickedDataProvider::getInstance();
        if (provider == nullptr)return;
        disconnect(provider, SIGNAL(sig_pickedOver()), this, SLOT(slotPickOverEvent()));
        QList<GraphData::PickedData*> pickList = provider->getPickedList();//拾取到那些对象的IDlist

        //计算获取到多少个几何对象
        for (GraphData::PickedData *data : pickList)
        {
            QList<int>temp = data->getPickedIds();
            m_pick_num += temp.size();
        }
        OnpickInProcess();
    }

    void GUIGeometryBaseDialog::OnpickInProcess()
    {
        //父类不实现
    }

    void GUIGeometryBaseDialog::reject()
    {
        if (_obj != nullptr)
        {
            delete _obj;
            _obj = nullptr;
        }
        GUI::GUIDialogBase::reject();
    }

}
